import pymysql
import json


def get_conn(mysql_json):
    conn = pymysql.connect(host=mysql_json.get('host'), port=mysql_json.get('port'),
                           user=mysql_json.get('user'), passwd=mysql_json.get('password'), db=mysql_json.get('db'),
                           charset='utf8')
    return conn


def execute(sql, mysql_json):
    conn = get_conn(mysql_json)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()


def main(begin_date, mysql_json_str):
    mysql_json = json.loads(mysql_json_str.replace("'", '"'))
    sql = """insert into t_order_source_spark(spark_date,spark_flag) values('{begin_date}','1')""".format(
        begin_date=begin_date)
    execute(sql, mysql_json)
